#include "Listado.h"

Listado::Listado(void)
{
}

vector<string> Listado::listarDirectorio()
{
	Texto* texto = new Texto();
		
	int iRC = 0;
	vector<string> vecXmlFiles;

	iRC = SearchDirectory(vecXmlFiles, "c:\\Escenarios", "xml");

	if(iRC) {
		cout << "Error " << iRC << endl;
		return vecXmlFiles;
	}

	for(vector<string>::iterator iterXml = vecXmlFiles.begin(); iterXml != vecXmlFiles.end(); ++iterXml) {
		cout << *iterXml << endl;
	}

	return vecXmlFiles;
}

int Listado::SearchDirectory(vector<string> &refvecFiles, const string &refcstrRootDirectory, const string &refcstrExtension) {
  string     strFilePath;             // Filepath
  string     strPattern;              // Pattern
  string     strExtension;            // Extension
  HANDLE          hFile;                   // Handle to file
  WIN32_FIND_DATA FileInformation;         // File information

  strPattern = refcstrRootDirectory + "\\*";

  hFile = ::FindFirstFile(strPattern.c_str(), &FileInformation);
  if(hFile != INVALID_HANDLE_VALUE) {
    do {
      if(FileInformation.cFileName[0] != '.') {
        strFilePath.erase();
        strFilePath = refcstrRootDirectory + "\\" + FileInformation.cFileName;

        // Check extension
        strExtension = FileInformation.cFileName;
        strExtension = strExtension.substr(strExtension.rfind(".") + 1);

        if(strExtension == refcstrExtension) {
			// Save filename
			size_t pos = strFilePath.find(".xml");    // position of "live" in str
			string subFileName = strFilePath.substr (14, pos - 14);
			refvecFiles.push_back(subFileName);
        }
      }
    } while(::FindNextFile(hFile, &FileInformation) == TRUE);

    // Close handle
    ::FindClose(hFile);

    DWORD dwError = ::GetLastError();

    if(dwError != ERROR_NO_MORE_FILES)
      return dwError;
  }

  return 0;
}
	
Listado::~Listado(void)
{
}